local wjsl_zhenta = fk.CreateSkill {
  name = "wjsl_zhenta",
  tags = { Skill.Compulsory, },
}

local CurPhaseEnd = function (target,event)
  if target.phase == Player.NotActive then return end
  local room = target.room
  local mark = target:getMark("@wjsl_zhenta_num")
  mark = mark - 1
  room:setPlayerMark(target,"@wjsl_zhenta_num",mark)
  if mark < 1 then
    room:doBroadcastNotify("ShowToast", "结束当前阶段")
    local current = target.room.logic:getCurrentEvent()
    local usecard_event = current:findParent(event)
    if not usecard_event then return end
    local phase_event = usecard_event:findParent(GameEvent.Phase)
    if not phase_event then return end
    usecard_event:addExitFunc(function()
      phase_event:shutdown()
    end)
  end
end

wjsl_zhenta:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wjsl_zhenta.name) then return end
    if player == target then return end
    return player.room.current == target
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local maxhp = target.maxHp
    if maxhp <= 5 then
      local value = 0
      if target.id > 0 and target.role == "rebel" then
        value = target.maxHp * 2
      end
      if target.id < 0 and target.role == "loyalist" then
        value = target.maxHp * 3
      end
      room:setPlayerMark(target,"@wjsl_zhenta_num",value)
    else
      local value = 0
      if target.id > 0 and target.role == "rebel" then
        value = math.min(10+(maxhp-5),15)
      end
      if target.id < 0 and target.role == "loyalist" then
        value = 15
      end
      room:setPlayerMark(target,"@wjsl_zhenta_num", value)
    end
    local shield_mark = target:getMark("wjsl_zhujia_shield")
    if shield_mark > 0 then
      room:changeShield(target, -shield_mark)
      room:setPlayerMark(target,"wjsl_zhujia_shield",0)
    end
  end,
})
wjsl_zhenta:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wjsl_zhenta.name) then return end
    if player == target then return end
    return player.room.current == target
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(target,"@wjsl_zhenta_num",0)
  end,
})

wjsl_zhenta:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wjsl_zhenta.name) then return end
    if player == target then return end
    return true
  end,
  on_use = function(self, event, target, player, data)
    CurPhaseEnd(target,GameEvent.UseCard)
  end,
})

wjsl_zhenta:addEffect(fk.AfterSkillEffect, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wjsl_zhenta.name) then return end
    if player == target then return end
    return data.skill.name == "wjsl_zhujia"
  end,
  on_use = function(self, event, target, player, data)
    CurPhaseEnd(target,GameEvent.SkillEffect)
  end,
})


return wjsl_zhenta